//{{{

function CalendarEvent() {
	this.name = "";
	this.tiddler = "";
	this.year = null;
	this.month = null;
	this.day = null;
	this.time = null;

	this.impose_time = function(t) {
		var hm_format = /\(d{1,2}):\(d{1,2})/;
		var now_format = /now\+(\d+)(h|m)/;
		var now = new Date();
		var hours = now.getHours();
		var minutes = now.getMinutes();
		if (time.match(hm_format) == null) {
			if (this.time == "now") {
				this.time = hours.toString() + ':' + minutes.toString();
			}
			if (time.match(now_format) != null) {
				var m = now_format.exec(t);
				if (RegExp.$2 == 'h')
					hours += parseInt(RegExp.$1);
				if (RegExp.$2 == 'm')
					minutes += parseInt(RegExp.$1);
				this.time = hours.toString() + ':' + minutes.toString();
			}
		} else {
			this.time = t;
		}
		if (this.year == null)
			this.year = now.getYear();
		if (this.month == null)
			this.year = now.getMonth();
		if (this.week == null)
			this.week = now.getWeek();
		if (this.day == null)
			this.day = now.getDate();
	};

	this.impose_day = function(d) {
		var yyyy_mm_dd = /(\d{4})-(\d{2})-(\d{2})/g;
		var mm_dd = /^(\d{2})-(\d{2})$/g;
		var wd = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)$/g;
		var today_format = /today\+(\d+)(d|w|m|y)/g;
		var next_format = /next \s+ (Mon|Tue|Wed|Thu|Fri|Sat|Sun)/g;
		var tomorrow_format = /tomorrow\+(\d+)(d|w|m|y)/g;

		var now = new Date();
		var hours = now.getHours();
		var minutes = now.getMinutes();

		if (yyyy_mm_dd.exec(d) != null) {
			this.year = parseInt(RegExp.$1);
			this.month = parseInt(RegExp.$2);
			this.day = parseInt(RegExp.$3);
		}

		if (mm_dd.exec(d) != null) {
			this.month = parseInt(RegExp.$1);
			this.day = parseInt(RegExp.$2);
		}

		if (today_format.exec(d) != null) {
			var today = new Date();
			var add_val = RegExp.$1;
			var add_unit = RegExp.$2;
			switch (add_unit) {
			case 'd':
				today.setDate(today.getDate() + add_val);
			case 'w':
				today.setDate(today.getDate() + 7 * add_val);
			case 'm':
				today.setMonth(today.getMonth() + add_val);
			case 'y':
				today.setYear(today.getYear() + add_val);
			}
		}

		if (this.year == null)
			this.year = now.getYear();
		if (this.month == null)
			this.year = now.getMonth();
		if (this.week == null)
			this.week = now.getWeek();
		if (this.time == null)
			this.time = hours.toString() + ':' + minutes.toString();
	};

	this.impose_month = function(m) {
		// TODO : add logic here
	};

	this.impose_year = function(y) {
		// TODO : add logic here
	};

	this.update_status = function() {
		// TODO : add logic here
	};

	this.dueThisYear = function() {
		// TODO : add logic here
	};

	this.dueThisMonth = function() {
		// TODO : add logic here
	};

	this.dueThisWeek = function() {
		// TODO : add logic here
	};

	this.dueThisDay = function() {
		// TODO : add logic here
	};

	this.dueThisTime = function() {
		// TODO : add logic here
	};
	
	this.overdue = function() {
		// TODO : add logic here
	};
	
	// TODO : do we really need this?
	this.getXML = function() {
		var xml = "<event";
		if (name != "")
			xml += ' name="' + this.name + '" >\n';
		xml += '  <year>' + this.year.toString() + '</year>\n';
		xml += '  <month>' + this.month.toString() + '</month>\n';
		xml += '  <week>' + this.week.toString() + '</week>\n';
		xml += '  <day>' + this.day.toString() + '</day>\n';
		xml += '  <time>' + this.time.toString() + '</time>\n';
		xml += '  <status>' + this.status + '</status>\n';
		xml += '</event>';
		return xml;
	};

	this.fetchByName = function(name, calendar) {
		if (calendar == undefined)
			calendar = "calendar.xml";
		var cal = store.getTiddler(calendar);
		// TODO : add logic here
	};

	this.addToCalendar = function(calendar) {
		if (calendar == undefined)
			calendar = "calendar.xml";
		var cal = store.getTiddler(calendar);
		// TODO : add logic here
	};

	this.isInCalendar = function(calendar) {
	};

}

function clearCalendar() {
	var calendar = store.getTiddler("calendar.xml");
	calendar.text = "";
	// TODO : add logic here
}

function getEventsFromCalendar(calendar) {
	if(calendar==undefined) calendar="calendar.xml";
	var events = new Array();
	// TODO : add logic here
	return events;
}

function CalendarRunway(calendar) {
	this.overdue = new Array();
	this.dueToday = new Array();
	this.dueThisWeek = new Array();
	this.dueThisMonth = new Array();
	this.dueThisYear = new Array();
	this.other = new Array();

	this.fetchFromCalendar = function(calendar)
	{
		if (calendat == undefined)
			calendar = "calendar.xml";
		var events = getEventsFromCalendar(calendar);
		for(var i=0;i<events.length;i++) {
			if(event.overdue())
				this.overdue.push(event);
			else if(event.dueToday())
				this.dueToday.push(event);
			else if(event.dueThisWeek()) 
				this.dueThisWeek.push(event);
			else if(event.dueThisMonth())
				this.dueThisMonth.push(event);
			else if(event.dueThisYear())
				this.dueThisYear.push(event);
			else
				this.other.push(event);				
		}
	};
	
	if(calendar!=undefined) this.fetchFromCalendar(calendar);
	
	function genEventTable(list) {
		var out='';
		// TODO add logic here
	}

	this.toString = function() {
		// TODO : add logic here
	};
	
	this.toTWiki = function() {
		var out="";
		if(this.overdue.length>0) {
			out+="!Overdue\n";
			for(var i=0;i<overdue.length;i++) {
				
			}
		}
	};
	
	this.toHTML = function() {
		// TODO : add logic here
	};
}

config.macros.event = {};
config.macros.event.handler = function(place, macroName, params, wikifier,
		paramString, tiddler) {
	if (params.length < 1 || (params.length - 1) % 2 != 0) {
		alert('event macro : Invalid number of parameters - '
				+ params.length.toString());
		return;
	}
	var nof_properties = (params.length - 1) / 2;
	var name = params[0];
	var event = new CalendarEvent();
	var override_index = params.indexOf("override");
	var override = override_index != -1 ? params[override_index + 1] : "no";
	event.name = name;
	if (!(event.isInCalendar() && override == "no")) {
		for ( var i = 0; i < nof_properties; i++) {
			var pname = params[1 + 2 * i];
			var pval = params[1 + 2 * i + 1];
			switch (pname) {
			case "day":
				event.imposeDay(pval);
				break;
			case "week":
				event.imposeWeek(pval);
				break;
			case "month":
				event.imposeMonth(pval);
				break;
			case "year":
				event.imposeYear(pval);
				break;
			}
		}
		event.addToCalendar();
	}
};

/*
 * event "urgent meeting" day "tomorrow" time "12:15"
 * 
 */

config.macros.runway = {};
config.macros.runway.handler = function(place, macroName, params, wikifier,
		paramString, tiddler) {
	var calendar = params[0]==undefined?"calendar.xml":params[0];
	var runway = new CalendarRunway(calendar);
	wikify(runway.toTWiki());
};

//}}}
